<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>echarts示例</title>
		    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
            <script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.0.2/echarts.common.js"></script>
	</head>

	<body>
		<div id="main" style="width: 600px;height:400px;"></div>
		<script type="text/javascript">
			var myChart = echarts.init(document.getElementById('main'));

			// 初始化图表选项
			myChart.setOption({
			    title: {
			        text: 'ECharts 数据异步加载示例'
			    },
			    tooltip: {},
			    legend: {
			        data: ['销量']
			    },
			    xAxis: {
			        data: []
			    },
			    yAxis: {},
			    series: [{
			        name: '销量',
			        type: 'bar',
			        data: []
			    }]
			});

			function fetchData() {
			    $.ajax({
			        url: '', // 改为你的数据接口地址
			        type:'post',
			        dataType: 'json',
					beforeSend: function (xhr, settings) {
						xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
					},
			        success: function(data) {
			        	console.log("接口返回的数据：",data)
			            myChart.setOption({
			                xAxis: {
			                    data: data.categories
			                },
			                series: [{
			                    name: '销量',
			                    data: data.values
			                }]
			            });
			        },
			        error: function(xhr, type, errorThrown) {
			            // 处理错误
			        }
			    });
			}

            fetchData()
<!--			// 设置定时器，定期更新数据-->
<!--			setInterval(function () {-->
<!--			    fetchData(); // 调用获取数据的函数-->
<!--			}, 5000); // 5000毫秒即5秒钟更新一次数据-->
		</script>
	</body>
</html>